﻿using System;

using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace GroupChallenge.PlayerAdminWP7
{
    public partial class MainPage : PhoneApplicationPage
    {
        private CameraCaptureTask _Camera = null;

        public MainPage()
        {
            InitializeComponent();

            Loaded += new System.Windows.RoutedEventHandler(MainPage_Loaded);
        }

        private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            PlayerPictureViewModel playerPictureViewModel = LayoutRoot.DataContext as PlayerPictureViewModel;
            if (null != playerPictureViewModel)
            {
                _Camera = new CameraCaptureTask();
                _Camera.Completed += (sender2, result) => playerPictureViewModel.StorePicture(result);
            }
        }

        private void TakePictureButton_Click(object sender, EventArgs e)
        {
            if (null != _Camera)
                _Camera.Show();
        }

        private void UploadPictureButton_Click(object sender, EventArgs e)
        {
            PlayerPictureViewModel playerPictureViewModel = LayoutRoot.DataContext as PlayerPictureViewModel;
            if (null != playerPictureViewModel)
            {
                Dispatcher.BeginInvoke(() => playerPictureViewModel.Upload(this.Dispatcher));
            }
        }
    }
}